#include "stdafx.h"
#include "ScrollBar.h"
#include "SkCanvas.h"
#include "WidgetWin.h"

ScrollBar::ScrollBar(View* parent, Type type)
	: View(parent)
	, type_(TYPE_VERTICAL)
	, m_length(0)
	, m_stubLength(0)
	, m_lastMouseX(0)
	, m_lastMouseY(0)
	, m_pointLocation(0)
	, position_(0)
{
	memset(&startArrow_, 0, sizeof(startArrow_));
	memset(&endArrow_, 0, sizeof(endArrow_));

	startArrow_.length = 16;
	endArrow_.length = 16;
}


ScrollBar::~ScrollBar()
{
}

void ScrollBar::setLength(int length)
{
	m_length = length;
}

int ScrollBar::getLength()
{
	return m_length;
}

bool ScrollBar::updateLayout()
{

	// unsigned int lastStubLength = m_stubLength;
	// 1. calculate stub length.
	m_stubLength = getContentLength() * getContentLength() / (getLength() + getContentLength());

	if (m_stubLength < 10)
	{
		m_stubLength = 10;
	}

	// 2. calculate stub position.
	m_pointLocation = (getContentLength() - m_stubLength) * position_;

	return true;
}

bool ScrollBar::paintBackground(SkCanvas* canvas)
{
	if (canvas)
	{
		canvas->FillRect(SkColor(62, 62, 66), 0, 0, Width(), Height());
		return true;
	}
	return false;
}

bool ScrollBar::paintFrontground(SkCanvas* canvas)
{
	if (canvas)
	{
		if (getType() == TYPE_HORIZONTAL)
		{
			// draw Stub
			canvas->FillRect(SkColor(104, 104, 104), m_pointLocation + startArrow_.length, 2, m_stubLength, Height() - 4);
			// draw arrow
			//Gdiplus::Point points[] = { Height() / 2
		}
		else
		{
			canvas->FillRect(SkColor(104, 104, 104), 2, m_pointLocation + startArrow_.length, Width() - 4, m_stubLength);
			{
				// draw start arrow
				gfx::Point points[] =
				{
					gfx::Point(5, 0),
					gfx::Point(0, 5),
					gfx::Point(10, 5)
				};
				canvas->FillPaths(SkColor(153, 153, 153), points, sizeof(points) / sizeof(points[0]), 2, 5);
			}
			{
				// draw end arrow
				gfx::Point points[] =
				{
					gfx::Point(0, 0),
					gfx::Point(10, 0),
					gfx::Point(5, 5)
				};
				canvas->FillPaths(SkColor(153, 153, 153), points, sizeof(points) / sizeof(points[0]), 2, getContentLength() + startArrow_.length + 5);
			}
		}


		return true;
	}
	return false;
}

/*
bool ScrollBar::paintBorder(SkCanvas* canvas)
{
	return false;
}
*/

bool ScrollBar::paintHover(SkCanvas* canvas)
{
	if (canvas)
	{
		if (getType() == TYPE_HORIZONTAL)
		{
			canvas->FillRect(SkColor(158, 158, 158), m_pointLocation + startArrow_.length, 2, m_stubLength, Height() - 4);
		}
		else
		{
			canvas->FillRect(SkColor(158, 158, 158), 2, m_pointLocation + startArrow_.length, Width() - 4, m_stubLength);
		}

		return true;
	}
	return false;
}

bool ScrollBar::paintFocus(SkCanvas* canvas)
{
	if (canvas)
	{
		if (getType() == TYPE_HORIZONTAL)
		{
			canvas->FillRect(SkColor(239, 235, 239), m_pointLocation + startArrow_.length, 2, m_stubLength, Height() - 4);
		}
		else
		{
			canvas->FillRect(SkColor(239, 235, 239), 2, m_pointLocation + startArrow_.length, Width() - 4, m_stubLength);
		}

		return true;
	}
	return false;
}

bool ScrollBar::processMouseEvent(MouseEvent& event)
{
	if (event.type == TYPE_TOUCH_CLICK)
	{
		m_lastMouseX = event.x;
		m_lastMouseY = event.y;

		return true;
	}
	else if (event.type == TYPE_TOUCH_MOVE && event.functionKey == FUNCTIONKEY_LBTN)
	{
		ATLTRACE("[%s][%d] %d - %d\r\n", __FUNCTION__, __LINE__, static_cast<int>(event.y), static_cast<int>(m_lastMouseY));

		m_pointLocation += (static_cast<int>(event.y) - static_cast<int>(m_lastMouseY));

		if (m_pointLocation < 0)
		{
			m_pointLocation = 0;
		}

		if (m_pointLocation > getMoveLength())
		{
			m_pointLocation = getMoveLength();
		}

		ATLTRACE("[%s][%d] %d\r\n", __FUNCTION__, __LINE__, m_pointLocation);
		position_ = static_cast<float>(m_pointLocation) / (getMoveLength());

		//TODO: this function can't be used in here.
		RECT rect;
		rect.left = X();
		rect.top = Y();
		rect.right = X() + Width();
		rect.bottom = Y() + Height();
		::InvalidateRect(WidgetWin::getInstance().getNativeHwnd(), &rect, FALSE);

		m_lastMouseX = event.x;
		m_lastMouseY = event.y;

		return true;
	}

	return false;
}

// |<<##########--------------------------------->>|
//   |-----------ContentLength-------------------|
int ScrollBar::getContentLength()
{
	int length = 0;
	if (getType()==TYPE_HORIZONTAL)
	{
		length = Width() - startArrow_.length - endArrow_.length;
		if (length < 0)
		{
			length = 0;
		}
	}
	else
	{
		length = Height() - startArrow_.length - endArrow_.length;
		if (length < 0)
		{
			length = 0;
		}
	}
	return length;
}

// |<<##########--------------------------------->>|
//        |-----------MoveLength-----------|
int ScrollBar::getMoveLength()
{
	int length = 0;
	if (getType() == TYPE_HORIZONTAL)
	{
		length = Width() - startArrow_.length - endArrow_.length - m_stubLength;
		if (length < 0)
		{
			length = 0;
		}
	}
	else
	{
		length = Height() - startArrow_.length - endArrow_.length - m_stubLength;
		if (length < 0)
		{
			length = 0;
		}
	}
	return length;
}
